﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using thinger.HeatingControlProject._4.实体类;
using thinger.DataConvertLib;
using System.Threading;

namespace thinger.HeatingControlProject._2.数据层
{
    public class PLCDataService
    {
        private S7NetLib S7netlib = null;
        /// <summary>
        /// 打开PLC连接
        /// </summary>
        /// <param name="info"></param>
        /// <exception cref="Exception"></exception>
        public void Connect(PlcInfo info)
        {
            try
            {
                this.S7netlib = new S7NetLib(info.CpuType, info.IPAddress, info.Rack, info.Slot);
                this.S7netlib.OpenPLC();
            }
            catch (Exception ex)
            {
                throw new Exception("PLC连接失败！原因："+ex.Message);
            }

        }
        /// <summary>
        /// 关闭PLC连接
        /// </summary>
        public void Disconnect()
        {
            if (this.S7netlib != null)
            {
                this.S7netlib.ClosePLC();
            }
        }
        /// <summary>
        /// 读取PLC数据
        /// </summary>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public PLCData GetPLCData()
        {
            try
            {
                int length = 36;
                byte[] result = this.S7netlib.ReadDataFromPLC(S7.Net.DataType.DataBlock,1,0,length);
                if(result != null && result.Length == length)
                {
                    PLCData data = new PLCData();
                    //解析布尔量
                    data.FeedPumpState = BitLib.GetBitFromByteArray(result, 0, 0);
                    data.AddPumpState = BitLib.GetBitFromByteArray(result, 0, 1);
                    data.AirPumpState = BitLib.GetBitFromByteArray(result, 0, 2);
                    data.CirclePumpState = BitLib.GetBitFromByteArray(result, 0, 3);
                    data.OutValveState = BitLib.GetBitFromByteArray(result, 0, 4);
                    data.SystemState = BitLib.GetBitFromByteArray(result, 0, 5);
                    //解析浮点数
                    data.PressureIn = FloatLib.GetFloatFromByteArray(result, 4);
                    data.PressureOut = FloatLib.GetFloatFromByteArray(result, 8);
                    data.TempIn = FloatLib.GetFloatFromByteArray(result, 12);
                    data.TempOut = FloatLib.GetFloatFromByteArray(result, 16);
                    data.PressureBoiler = FloatLib.GetFloatFromByteArray(result, 20);
                    data.LevelBoiler = FloatLib.GetFloatFromByteArray(result, 24);
                    data.TempBoiler = FloatLib.GetFloatFromByteArray(result, 28);
                    data.LevelTank = FloatLib.GetFloatFromByteArray(result, 32);
                    return data;
                }
                else
                {
                    throw new Exception("PLC读取数据不正确！");
                }
            }
            catch (Exception ex)
            {

                throw new Exception("PLC读取数据失败！原因"+ ex.Message);
            }
        }

        /// <summary>
        /// 进水泵启停控制
        /// </summary>
        /// <param name="state"></param>
        /// <exception cref="Exception"></exception>
        public void FeedPumpControl(bool state)
        {
            if (this.S7netlib != null)
            {
                string address = state ? "DB1.DBX100.0" : "DB1.DBX100.1";
                this.S7netlib.WriteDataToPLC(address, true);
                Thread.Sleep(100);
                this.S7netlib.WriteDataToPLC(address, false);

            }
            else
            {
                throw new Exception("PLC未连接！");
            }
        }
        /// <summary>
        /// 加水泵启停控制
        /// </summary>
        /// <param name="state"></param>
        /// <exception cref="Exception"></exception>
        public void AddPumpControl(bool state)
        {
            if(this.S7netlib != null)
            {
                string address = state?"DB1.DBX100.2":"DB1.DBX100.3";
                this.S7netlib.WriteDataToPLC(address, true);
                Thread.Sleep(100);
                this.S7netlib.WriteDataToPLC(address, false);

            }
            else
            {
                throw new Exception("PLC未连接！");
            }
        }

        /// <summary>
        /// 燃气泵启停控制
        /// </summary>
        /// <param name="state"></param>
        /// <exception cref="Exception"></exception>
        public void AirPumpControl(bool state)
        {
            if (this.S7netlib != null)
            {
                string address = state ? "DB1.DBX100.4" : "DB1.DBX100.5";
                this.S7netlib.WriteDataToPLC(address, true);
                Thread.Sleep(100);
                this.S7netlib.WriteDataToPLC(address, false);

            }
            else
            {
                throw new Exception("PLC未连接！");
            }
        }

        /// <summary>
        /// 循环泵启停控制
        /// </summary>
        /// <param name="state"></param>
        /// <exception cref="Exception"></exception>
        public void CirclePumpControl(bool state)
        {
            if (this.S7netlib != null)
            {
                string address = state ? "DB1.DBX100.6" : "DB1.DBX100.7";
                this.S7netlib.WriteDataToPLC(address, true);
                Thread.Sleep(100);
                this.S7netlib.WriteDataToPLC(address, false);

            }
            else
            {
                throw new Exception("PLC未连接！");
            }
        }

        /// <summary>
        /// 出水阀启停控制
        /// </summary>
        /// <param name="state"></param>
        /// <exception cref="Exception"></exception>
        public void OutValvePumpControl(bool state)
        {
            if (this.S7netlib != null)
            {
                string address = state ? "DB1.DBX101.0" : "DB1.DBX101.1";
                this.S7netlib.WriteDataToPLC(address, true);
                Thread.Sleep(100);
                this.S7netlib.WriteDataToPLC(address, false);

            }
            else
            {
                throw new Exception("PLC未连接！");
            }
        }


    }
}
